<?php

add_action( 'wp', 'bpdev_theme_extra_setup_globals', 2 );
add_action( 'admin_menu', 'bpdev_theme_extra_setup_globals', 2 );

add_action( 'bpdev_theme_admin_menu_extra_screen' , 'bpdev_theme_extra_css_menu_screen' , 1 );
add_action( 'bpdev_theme_admin_menu_extra_save' , 'bpdev_theme_extra_css_menu_save' , 1 );

function bpdev_theme_register_style( $slug, $name, $callback, $default = 'off', $admin_show = 'on', $condition = true, $priority = 1 ) {

	global $bp;

	if ( $condition ) {

		$bp->bpdev->theme->extra->{$slug}->slug = $slug;
		$bp->bpdev->theme->extra->{$slug}->name = $name;
		$bp->bpdev->theme->extra->{$slug}->callback = $callback;
		$bp->bpdev->theme->extra->{$slug}->default = $default;
		$bp->bpdev->theme->extra->{$slug}->priority = $priority + 2;
		$bp->bpdev->theme->extra->{$slug}->admin = $admin_show;

	}

}

function bpdev_theme_extra_setup_globals() {

	global $bp;

	bpdev_theme_register_style(
		'userbar-ul-li', 
		'Rounded #userbar ul li', 
		array( 'BPDEV_Theme_CSS' , 'userbar_ul_li' )
	);

	bpdev_theme_register_style(
		'search-login-bar', 
		'Search login bar', 
		array( 'BPDEV_Theme_CSS' , 'search_login_bar' )
	);

	do_action( 'bpdev_theme_extra_setup_globals' );

}

function bpdev_theme_extra_css_cond() {

	global $bp;

	require_once( 'bpdev-theme-extra.php' );

	foreach ( $bp->bpdev->theme->extra as $extra ) {

		if ( !get_option( "bpdev-theme-{$extra->slug}-status" ) )
			add_option( "bpdev-theme-{$extra->slug}-status",  $extra->default );

		if ( 'on' == get_option( "bpdev-theme-{$extra->slug}-status" ) )
			add_action( 'bpdev_theme_css' , $extra->callback , $extra->priority );

	}

}

function bpdev_theme_extra_css_menu_screen() {

	global $bp;

	foreach ( $bp->bpdev->theme->extra as $extra ) {

		if ( 'on' == $extra->admin ) {

			?>

	<tr>
		<th scope="row"><?php echo $extra->name ?></th>
		<td>
			<select id="bpdev-theme-<?php echo $extra->slug ?>-status" name="bpdev-theme-<?php echo $extra->slug ?>-status">
				<option value="off" <?php if ( get_option( "bpdev-theme-{$extra->slug}-status" ) == "off" ) echo 'selected=""'; ?>>Off</option>
				<option value="on" <?php if (  get_option( "bpdev-theme-{$extra->slug}-status" ) == "on") echo 'selected=""'; ?>>On</option>
			</select>
		</td>
	</tr>

			<?php

		}

	}

}

function bpdev_theme_extra_css_menu_save() {

	global $bp;

	foreach ( $bp->bpdev->theme->extra as $extra ) {

		if ( 'on' == $extra->admin )
			do_action( 'bpdev_admin_save_component', "bpdev-theme-{$extra->slug}-status" );

	}

}

function bpdev_theme_logo_adminbar() {

	return get_option( 'bpdev-theme-buddybar-logo' );

}

?>